package com.ijiuchuang.jc_localization.view.activity;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import com.ijiuchuang.jc_localization.R;
import com.ijiuchuang.jc_localization.app.Config;
import com.litesuits.orm.LiteOrm;
import com.litesuits.orm.db.DataBaseConfig;

/**
 * An example full-screen activity that shows and hides the system UI (i.e.
 * status bar and navigation/system bar) with user interaction.
 */
public class LauncherActivity extends AppCompatActivity {

    private static final int GO_TO_MAIN = 0x01;
    private static final long GUIDE_DELAY = 1500;

    private static final int WRITE_EXTERNAL_STORAGE_REQUEST_CODE = 0;
    // LiteOrm
    static LiteOrm liteOrm;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_launcher);

        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.hide();
        }

        //申请WRITE_EXTERNAL_STORAGE权限
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                    WRITE_EXTERNAL_STORAGE_REQUEST_CODE);
        } else {
            goWithPermission();
        }
    }

    private void goWithPermission() {
        initLiteOrm();

        handler.sendEmptyMessageDelayed(GO_TO_MAIN, GUIDE_DELAY);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        doNext(requestCode,grantResults);
    }

    private void doNext(int requestCode, int[] grantResults) {
        if (requestCode == WRITE_EXTERNAL_STORAGE_REQUEST_CODE) {
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                // Permission Granted
                goWithPermission();
            } else {
                // Permission Denied
                finish();
            }
        }
    }

    private void initLiteOrm() {
        /**
         * LiteOrm 初始化
         * 保持单例
         * 调试阶段： DB文件放在存储卡  TODO: 2016/6/17 发布应用时，db文件位置不要放在内存卡
         */
        if (liteOrm == null) {
            DataBaseConfig config = new DataBaseConfig(this, Config.DB_NAME);
            config.dbVersion = 1;
            config.debugged = true;
            config.onUpdateListener = null;
            liteOrm = LiteOrm.newSingleInstance(config);
        }
        Config.liteOrm = liteOrm;
    }

    private Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            switch (msg.what) {
                case GO_TO_MAIN:
                    startActivity(new Intent(LauncherActivity.this, HomeActivity.class));
                    finish();
                    break;
            }
            return false;
        }
    });
}
